package com.xiaozeng.category.controller;


import com.xiaozeng.category.service.CategoryService;
import com.xiaozeng.param.ProductHotParam;
import com.xiaozeng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xiaozeng
 * @Date: 2022 11
 * @Description:
 **/
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据类别名字查询类别ID
     * @param categoryName 类别名字
     */
    @GetMapping("/promo/{categoryName}")
    public Result queryByName(@PathVariable String categoryName){
        if(StringUtils.isEmpty(categoryName)) return Result.fail("类别为空");
        return categoryService.queryByName(categoryName);
    }

    /**
     * 根据类别名字查询类别ID
     * @param productHotParam 类别名字
     */
    @PostMapping("hots")
    public Result hots(@RequestBody @Validated ProductHotParam productHotParam, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.fail("数据查询失败!");
        }

        return categoryService.hotsCategory(productHotParam);
    }

    @GetMapping("list")
    public Result list(){
        return categoryService.list();
    }


}
